package com.wzq.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sun.misc.BASE64Decoder;

import com.wzq.bean.MonomerCollectionModel;
import com.wzq.service.MonomerCollectionService;
import com.wzq.util.ApiException;
import com.wzq.util.ApiResponse;
import com.wzq.util.Pager;
import com.wzq.util.ResponseCode;
import com.wzq.util.UploadPicUtil;

@Controller 
@RequestMapping("monomercollection/")
public class MonomerCollectionController{
	@Autowired
	private MonomerCollectionService monomerCollectionService;
	private static final Logger logger = LogManager.getLogger(MonomerCollectionController.class);

	@RequestMapping("dataList")
	@ResponseBody
	public ApiResponse<List<MonomerCollectionModel>> dataList(MonomerCollectionModel model,HttpServletRequest request,Pager pager)throws ApiException{
		ApiResponse<List<MonomerCollectionModel>> response = new  ApiResponse<List<MonomerCollectionModel>>(ResponseCode.SUCC);       
		List<MonomerCollectionModel> list = monomerCollectionService.queryList(model, pager);
		response.setData(list);	
		return response;
	}
	
	
	@RequestMapping("add")
	@ResponseBody
	public ApiResponse<String> add(MonomerCollectionModel model,HttpServletRequest request)throws ApiException{
		ApiResponse<String> response = new  ApiResponse<String>(ResponseCode.SUCC);
		monomerCollectionService.add(model);
		//上传图片到图片目录
		UploadPicUtil.uploadPic(model.getPicFileStr(), model.getPic(), request.getRealPath("/")+"/picPath");
		return response;
	}
	
	@RequestMapping("update")
	@ResponseBody
	public ApiResponse<String> update(MonomerCollectionModel model,HttpServletRequest request)throws ApiException{
		ApiResponse<String> response = new  ApiResponse<String>(ResponseCode.SUCC);
		monomerCollectionService.updateBySelective(model);
		return response;
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public ApiResponse<String> delete(Integer id,HttpServletRequest request)throws ApiException{
		ApiResponse<String> response = new  ApiResponse<String>(ResponseCode.SUCC);
		monomerCollectionService.delete(id);
		return response;
	}
	
	@RequestMapping("getById")
	@ResponseBody
	public ApiResponse<MonomerCollectionModel> getById(Integer id,HttpServletRequest request,Pager pager)throws ApiException{
		ApiResponse<MonomerCollectionModel> response = new  ApiResponse<MonomerCollectionModel>(ResponseCode.SUCC);       
		MonomerCollectionModel model = monomerCollectionService.getById(id);
		response.setData(model);	
		return response;
	}
	
	
}
